package cn.b.sky.mes.mm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.WarehouseType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.WarehouseTypeCondition;
import cn.b.sky.mes.mm.service.WarehouseTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : WarehouseType.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>仓库类型控制器<br> 
 * <strong>Create on : 2016年2月29日 上午11:04:44<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br> 
 * <strong>修改历史:</strong><br>
 * 修改人    |    修改日期    |    修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/warehouseType/*")
public class WarehouseTypeController extends SecurityController {
  @Resource
  WarehouseTypeService warehouseTypeService;

  @RequestMapping("warehouseTypeList")
  public String selectDefectTypeListByCondition(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    return "mm/warehouseType/warehouseTypeList";
  }

  @RequestMapping("selectWarehouseTypeList")
  @ResponseBody
  public DWZResult selectDefectTypeList(@ModelAttribute("condition") WarehouseTypeCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<WarehouseType> list = warehouseTypeService.selectWarehouseTypeListByCondition(condition);
    int count = warehouseTypeService.selectWarehouseTypeCountByCondition(condition);
    condition.setTotalCount(count);
    condition.setData(list);
    return ajaxDone(condition);
  }

  @RequestMapping("addWarehouseType")
  public String addBom(Model model, String navTabId) {
    model.addAttribute("navTabId", navTabId);
    return "mm/warehouseType/warehouseType_add";
  }

  @RequestMapping("editWarehouseType")
  public String editWarehouseType(Model model, String navTabId, String id) {
    WarehouseType warehouseType = warehouseTypeService.getWarehouseType(id, getSiteCode());
    model.addAttribute("warehouseType", warehouseType);
    model.addAttribute("navTabId", navTabId);
    return "mm/warehouseType/warehouseType_add";
  }

  @RequestMapping("viewWarehouseType")
  public String viewWarehouseType(Model model, String navTabId, String id) {
    WarehouseType warehouseType = warehouseTypeService.getWarehouseType(id, getSiteCode());
    model.addAttribute("warehouseType", warehouseType);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("pattern", "view");
    return "mm/warehouseType/warehouseType_add";
  }

  @RequestMapping("saveWarehouseType")
  @ResponseBody
  public DWZResult saveWarehouseType(Model model, String navTabId, String id, WarehouseType warehouseType) {
    if (warehouseType == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    if (warehouseType.getLockFlag() == null) {
      warehouseType.setLockFlag(0);
    }
    setCreateInfo(warehouseType);
    warehouseType.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      warehouseTypeService.insertWarehouseType(warehouseType);
    } else {
      warehouseTypeService.updateWarehouseType(warehouseType);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deleteWarehouseType")
  @ResponseBody
  public DWZResult deleteWarehouseType(Model model, String navTabId, HttpServletRequest request) {

    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] codes = ids.split(",");
    int len = codes.length;
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = warehouseTypeService.deleteWarehouseType(codes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("warehouseTypeLookUp")
  public String warehouseTypeLookUp(Model model, String lookupType,
      @ModelAttribute("condition") WarehouseTypeCondition condition, String type) {
    condition.setSiteCode(getSiteCode());
    if (!"all".equals(type)) {
      condition.setLockFlag(0);
    }
    List<WarehouseType> list = warehouseTypeService.selectWarehouseTypeListByCondition(condition);
    int count = warehouseTypeService.selectWarehouseTypeCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "mm/warehouseType/warehouseType_lookup";
  }

  @RequestMapping("warehouseTypeSuggest")
  @ResponseBody
  public List<WarehouseType> warehouseTypeSuggest(@ModelAttribute("condition") WarehouseTypeCondition condition,
      String type) {
    condition.setSiteCode(this.getSiteCode());
    if (!"all".equals(type)) {
      condition.setLockFlag(0);
    }
    return warehouseTypeService.selectWarehouseTypeSuggests(condition);
  }

  @RequestMapping(value = "validateWarehouseTypeCode")
  @ResponseBody
  public boolean validateWarehouseTypeCode(String warehouseTypeCode, String orgWarehouseTypeCode) {
    if (orgWarehouseTypeCode != null && orgWarehouseTypeCode.equals(warehouseTypeCode)) {
      return true;
    }
    if (!StringUtils.isEmpty(warehouseTypeCode)) {
      WarehouseType warehouseType = warehouseTypeService.getWarehouseType(warehouseTypeCode, getSiteCode());

      return warehouseType != null && warehouseType.getLockFlag() != 1;
    }
    return false;
  }

  @RequestMapping(value = "checkWarehouseTypeCode")
  @ResponseBody
  public boolean checkWarehouseTypeCodeNotExists(String warehouseTypeCode) {
    if (!StringUtils.isEmpty(warehouseTypeCode)) {
      WarehouseType warehouseType = warehouseTypeService.getWarehouseType(warehouseTypeCode, getSiteCode());
      return warehouseType == null;
    }
    return true;
  }

}
